import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import '../../../app_files.dart';
import 'action.dart';
import 'state.dart';
import '../model/account_model.dart';

Widget buildView(
    UserAccountViewState state, Dispatch dispatch, ViewService viewService) {
  Widget _buildItem(BuildContext context, int index) {
    GasAccountModel item = state.items[index];
    Widget _body() => GestureDetector(
          child: Container(
            margin: EdgeInsets.only(
              top: 10,
            ),
            padding: EdgeInsets.symmetric(horizontal: 15),
            color: Colors.white,
            child: Row(
              children: <Widget>[
                item.isICUser
                    ? Image.asset('images/i_icCard.png')
                    : Image.asset('images/icon_gas.png'),
                Expanded(
                  child: Container(
                    padding: EdgeInsets.only(left: 16),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        item.isICUser && (state.type == 0 || state.type == 3)
                            ? Row(
                                children: [
                                  Text(
                                    item.title,
                                    style:
                                        TextThemeConfig.Font16BoldWithColor22,
                                  ),
                                  Expanded(child: Container()),
                                  item.isXABleUser
                                      ? Container()
                                      : MyInkWellWidget(
                                          child: item.bluetoothNo.length != 0
                                              ? Row(
                                                  children: [
                                                    Padding(
                                                      padding: EdgeInsets.only(
                                                          right: 5),
                                                      child: Image.asset(
                                                          'images/gray_blue_tooth.png'),
                                                    ),
                                                    Text(
                                                      item.bluetoothNo,
                                                      style: TextThemeConfig
                                                          .Font13WithColor99,
                                                    ),
                                                  ],
                                                )
                                              : Row(
                                                  children: [
                                                    Padding(
                                                      padding: EdgeInsets.only(
                                                          right: 5),
                                                      child: Image.asset(
                                                          'images/ic_blue_tooth.png'),
                                                    ),
                                                    Text(
                                                      '可绑定蓝牙读卡器',
                                                      style: TextThemeConfig
                                                          .Font13WithColorblue,
                                                    ),
                                                  ],
                                                ),
                                          onTap: item.bluetoothNo.length != 0
                                              ? null
                                              : () =>
                                                  CommonUtils.pushRoute(
                                                      viewService.context,
                                                      RoutePath
                                                          .bindBlueToothView,
                                                      arguments: {
                                                        'userID': item.gasNo
                                                      }),
                                        ),
                                ],
                              )
                            : Text(
                                item.title,
                                style: TextThemeConfig.Font16BoldWithColor22,
                              ),
                        Padding(padding: EdgeInsets.only(top: 6)),
                        Text(
                          item.gasNo,
                          style: TextThemeConfig.Font14WithColor99,
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
          onTap: () {
            if (state.type == 0) {
              CommonUtils.pushRoute(
                  viewService.context, RoutePath.accountInfoView,
                  arguments: {
                    'isDel': true,
                    'accountNo': item.gasNo,
                    'accountName': item.title,
                    'blindID': item.itemId,
                  });
            }
            if (state.type == 1) {
              CommonUtils.pushReplaceRoute(
                  viewService.context, RoutePath.sendReportView,
                  arguments: {
                    'accountNo': item.gasNo,
                    'accountName': item.title,
                  });
            }
            if (state.type == 2) {
              CommonUtils.pushReplaceRoute(
                  viewService.context, RoutePath.safeOrderView,
                  arguments: {'gasNo': item.gasNo});
            }
            if (state.type == 3) {
              if (item.isICUser)
                CommonUtils.pushReplaceRoute(
                    viewService.context, RoutePath.gasChargeView,
                    arguments: {'model': item});
              else
                CommonUtils.showErrorInfo(viewService.context, '该户暂不支持app购气充值');
            }
            if (state.type == 4) {
              if (state.nbCtrlCodes.contains(item.ctrlCode)) {
                CommonUtils.pushRoute(
                    viewService.context, RoutePath.nbOrderView,
                    arguments: {'userID': item.userID});
              }
              if (state.machineCtrlCodes.contains(item.ctrlCode)) {
                CommonUtils.pushRoute(
                    viewService.context, RoutePath.machineOrderView,
                    arguments: {'userID': item.userID});
              }
            }
          },
        );

    return state.type == 0
        ? MyDismissWidget(
            child: _body(),
            key: Key('key_${item.itemId}'),
            onDelete: () {
              dispatch(UserAccountViewActionCreator.onDelete(item));
            },
            confirmDismissMessage: '确定解除户号绑定？',
          )
        : _body();
  }

  return Scaffold(
    appBar: myAppBar(viewService.context,
        title: state.type == 0 ? '户号管理' : '选择户号',
        actions: state.type == 0
            ? [
                GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  child: Container(
                    padding: EdgeInsets.only(right: 12),
                    height: 40,
                    child: Center(
                      child: Text(
                        '添加',
                        style: TextThemeConfig.ActionBarTitle,
                      ),
                    ),
                  ),
                  onTap: () => CommonUtils.pushRoute(
                      viewService.context, RoutePath.bindAccountView),
                ),
              ]
            : null),
    backgroundColor: ThemeConfig.separeColor,
    body: state.items == null
        ? MyLoadingWidget()
        : ListView.builder(
            itemBuilder: _buildItem,
            itemCount: state.items.length,
            itemExtent: 85,
          ),
  );
}
